package org.nettyServer.server;

import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.handler.timeout.IdleStateEvent;
import lombok.extern.slf4j.Slf4j;
import org.nettyServer.command.heartbeat.SC_Heartbeat;
import org.nettyServer.eunm.EMessageCode;
import org.nettyServer.messageHandle.PushUtils;

@Slf4j
public class IdleHandler extends ChannelInboundHandlerAdapter {
    @Override
    public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
        // IdleStateHandler 所产生的 IdleStateEvent 的处理逻辑.
        if (evt instanceof IdleStateEvent) {
            IdleStateEvent e = (IdleStateEvent) evt;
            switch (e.state()) {
                case READER_IDLE:
                    System.out.println("READER_IDLE");
                    break;
                case WRITER_IDLE:
                    System.out.println("WRITER_IDLE");
                    break;
                case ALL_IDLE:
                    PushUtils.pushMessage(EMessageCode.HEARTBEAT.getCode(), new SC_Heartbeat(), ctx.channel());
                    break;
                default:
                    break;
            }
        }
    }
}
